package com.yuantong.xyerp.core.provider.dao;


import com.yuantong.xyerp.core.api.dto.MerchantDto;
import com.yuantong.xyerp.core.api.model.Merchant;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 往来单位（供应商、客户、代理商） Mapper 接口
 * </p>
 *
 * @author JianguoYin
 * @since 2021-07-21
 */
public interface MerchantMapper {

    // getMerchant(); //<script></script>
    @Insert({"<script> " +
            "INSERT INTO t_merchant (id, `number`, `name`, `type`, ext_agent, category_code_list, province_id, city_id, " +
            "area_id, address, postcode, remarks, contacts_name, contacts_mobile, contacts_department, " +
            "contacts_position, contacts_email, invoice_name, invoice_code, invoice_bank, invoice_account, invoice_tele, " +
            "invoice_address, operator_id, update_time, creator_id, create_time, status, delete_status ) " +
            "VALUES (#{id},#{number},#{name},#{type},#{extAgent},#{categoryCodeList},#{provinceId},#{cityId},#{areaId}, " +
            "#{address},#{postcode},#{remarks},#{contactsName},#{contactsMobile},#{contactsDepartment}, " +
            "#{contactsPosition},#{contactsEmail},#{invoiceName},#{invoiceCode},#{invoiceBank},#{invoiceAccount}," +
            "#{invoiceTele},#{invoiceAddress},#{operatorId},ifnull(#{updateTime}, current_timestamp),#{creatorId}, " +
            "ifnull(#{createTime}, current_timestamp),#{status},#{deleteStatus}) " +
            "</script>"})
    @Options(useGeneratedKeys = false)
    int addMerchant(Merchant merchant);

    @Update({"<script> " +
            "UPDATE t_merchant " +
            "<set> " +
            "<if test=\"number != null\"> " +
            "    number = #{number}, " +
            "</if> " +
            "<if test=\"name != null\"> " +
            "    name = #{name}, " +
            "</if> " +
            "<if test=\"type != null\"> " +
            "    type = #{type}, " +
            "</if> " +
            "<if test=\"extAgent != null\"> " +
            "    ext_agent = #{extAgent}, " +
            "</if> " +
            "<if test=\"categoryCodeList != null\"> " +
            "    category_code_list = #{categoryCodeList}, " +
            "</if> " +
            "<if test=\"provinceId != null\"> " +
            "    province_id = #{provinceId}, " +
            "</if> " +
            "<if test=\"cityId != null\"> " +
            "    city_id = #{cityId}, " +
            "</if> " +
            "<if test=\"areaId != null\"> " +
            "    area_id = #{areaId}, " +
            "</if> " +
            "<if test=\"address != null\"> " +
            "    address = #{address}, " +
            "</if> " +
            "<if test=\"postcode != null\"> " +
            "    postcode = #{postcode}, " +
            "</if> " +
            "<if test=\"remarks != null\"> " +
            "    remarks = #{remarks}, " +
            "</if> " +
            "<if test=\"contactsName != null\"> " +
            "    contacts_name = #{contactsName}, " +
            "</if> " +
            "<if test=\"contactsMobile != null\"> " +
            "    contacts_mobile = #{contactsMobile}, " +
            "</if> " +
            "<if test=\"contactsDepartment != null\"> " +
            "    contacts_department = #{contactsDepartment}, " +
            "</if> " +
            "<if test=\"contactsPosition != null\"> " +
            "    contacts_position = #{contactsPosition}, " +
            "</if> " +
            "<if test=\"contactsEmail != null\"> " +
            "    contacts_email = #{contactsEmail}, " +
            "</if> " +
            "<if test=\"invoiceName != null\"> " +
            "    invoice_name = #{invoiceName}, " +
            "</if> " +
            "<if test=\"invoiceCode != null\"> " +
            "    invoice_code = #{invoiceCode}, " +
            "</if> " +
            "<if test=\"invoiceBank != null\"> " +
            "    invoice_bank = #{invoiceBank}, " +
            "</if> " +
            "<if test=\"invoiceAccount != null\"> " +
            "    invoice_account = #{invoiceAccount}, " +
            "</if> " +
            "<if test=\"invoiceTele != null\"> " +
            "    invoice_tele = #{invoiceTele}, " +
            "</if> " +
            "<if test=\"invoiceAddress != null\"> " +
            "    invoice_address = #{invoiceAddress}, " +
            "</if> " +
            "<if test=\"operatorId != null\"> " +
            "    operator_id = #{operatorId}, " +
            "</if> " +
            "<if test=\"updateTime != null\"> " +
            "    update_time = #{updateTime}, " +
            "</if> " +
            "<if test=\"creatorId != null\"> " +
            "    creator_id = #{creatorId}, " +
            "</if> " +
            "<if test=\"createTime != null\"> " +
            "    create_time = #{createTime}, " +
            "</if> " +
            "<if test=\"status != null\"> " +
            "    status = #{status}, " +
            "</if> " +
            "<if test=\"deleteStatus != null\"> " +
            "    delete_status = #{deleteStatus}, " +
            "</if>" +
            "</set> " +
            "WHERE id = #{id}" +
            "</script>"})
    int update(Merchant merchant);

    //todo: 控制器使用时, 记得手动添加 categoryNames
    @Select({"<script> " +
            "SELECT m.*, u.real_name AS operator_name FROM t_merchant m " +
            // "LEFT JOIN t_area a ON (m.province_id = a.code OR m.city_id = a.code OR m.area_id = a.code) " +
            "LEFT JOIN t_user u ON (u.id = m.operator_id) " +
            "WHERE 1=1 " +
            "<if test=\"merchantType != null and merchantType != ''\"> " +
            "    AND type = #{merchantType} " +
            "</if> " +
            "<if test=\"startTime != null\"> " +
            "  AND create_time &gt;= #{startTime} " +
            "</if> " +
            "<if test=\"endTime != null\"> " +
            "  AND create_time &lt;= #{endTime} " +
            "</if> " +
            "<if test=\"fuzzy != null and fuzzy != ''\"> " +
            "<bind name=\"bindKey\" value=\"'%'+fuzzy+'%'\"/> " +
            "AND (m.name LIKE #{bindKey} OR m.number LIKE #{bindKey} OR m.contacts_name LIKE #{bindKey}) " +
            "</if> " +
            "<if test=\"ids != null and ids.size() > 0\"> " +
            "AND m.id IN " +
            "<foreach collection=\"ids\" item=\"id\" open=\"(\" close=\")\" separator=\",\"> " +
            "#{id} " +
            "</foreach> " +
            "</if> " +
            "ORDER BY create_time DESC " +
            "</script>"})
    @ResultMap("MerchantDtoResult")
    List<MerchantDto> findAllByFuzzy(String merchantType,
                                     Date startTime, Date endTime,
                                     String fuzzy,
                                     List<String> ids);


}
